package com.yt.base.util;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

public class CustomerPropertyPlaceholderConfigurer extends
		PropertyPlaceholderConfigurer {
	private Logger log = LoggerFactory.getLogger(CustomerPropertyPlaceholderConfigurer.class);
	public void setLocations(Resource[] locations) {
		if (locations != null) {
			String userDir=System.getProperty("user.dir");
			for (int i=0;i<locations.length;i++) {
				try {
					Resource location = locations[i];
					String fileName=location.getFile().getPath();
					String fileNameNoPath=location.getFile().getName();
					if(fileName.startsWith("${user_dir}")){
						log.info("开始重新定位资源"+location);
						try {
							location=new UrlResource("file://"+userDir+fileName.substring("${user_dir}".length(),fileName.length()));
							locations[i]=location;
							if(!location.getFile().exists()){
								throw new IOException("没有找到文件"+fileNameNoPath);
							}
							log.info("重新定位资源"+location+"成功");
						} catch (Exception e) {
							log.info("重新定位资源"+location+"失败:"+e.getMessage());
							log.info("开始重新定位资源"+location+"尝试从classpath中加载");
							location=new ClassPathResource(fileNameNoPath);
							locations[i]=location;
							log.info("开始重新定位资源"+location+"尝试从classpath中加载成功");
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			super.setLocations(locations);
		}	
	}
}
